namespace taurus.client{

    /**
     * 网络管理对象
     */
    export class NetManager{
        /**
         * 网络超时时间
         */
        public static 			TIMEOUT_TIME:number	= 10;
        /**
         * 网络版本号
         */
        public static  			VERSION:number			= 10;
        public static 		    debug_print:boolean 	= true;
        private static _clientMap:any = {};
        private static _clientList:TaurusClient[] = [];


        static addNetClient(net:TaurusClient) {
            if (net == null) {
                throw "TaurusClient is null, it can't be added.";
            }
            if (NetManager._clientMap[net.getId()]) {
                throw "A client with id: " + net.getId() + " already exists! Controller can't be added: " + net;
            }
           
            this._clientMap[net.getId()] = net;
            this._clientList.push(net);
        }

        /**
         * 返回指定的NetClient
         * @param id
         * @return
         */
        public static  getNetClient( id:number):TaurusClient{
            for (var i = 0; i < this._clientList.length; ++i) {
                var nc = this._clientList[i];
                if (nc.getId() == id)
                    return nc;
            }
            return null;
        }
    
        /**
         * 断开指定连接
         * @param id
         */
        public static killConnection(id:number) {
            for (var i = 0; i < this._clientList.length; ++i) {
                var nc = this._clientList[i];
                if (nc.getId() == id) {
                    nc.killConnection();
                    delete this._clientMap[id];
                    this._clientList.splice(i,1);
                    break;
                }
            }
        }
    
        /**
         * 断开所有连接
         */
        public static killAllConnection() {
            for (var nc of this._clientList) {
                nc.killConnection();
            }
            this._clientList = [];
            this._clientMap = {};
        }
    }
}